package com.cspg.ssss.service;

import com.cspg.ssss.domain.SecurityRiskEntity;
import com.cspg.ssss.domain.SecurityRiskLogEntity;
import com.cspg.ssss.repository.SecurityRiskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安风体系评级服务
 */
@Service
public class SecurityRiskService {
    @Autowired
    private SecurityRiskMapper riskMapper;

    public List<SecurityRiskEntity> selectList() {
        List<SecurityRiskEntity> list = riskMapper.selectList();
        Map<String, Object> map = new HashMap<>();
        map.put("type", 1);
        for (SecurityRiskEntity entity : list) {
            int num = riskMapper.selectWarring(entity.getDwmc());
            if (num == 0) {
                entity.setWarring(0);
            } else if (num <= 3) {
                entity.setWarring(3);
            } else if (num <= 10) {
                entity.setWarring(2);
            } else {
                entity.setWarring(1);
            }
            map.put("id", entity.getId());
            List<SecurityRiskLogEntity> logList = riskMapper.selectLogList(map);
            if (logList.size() > 1) {
                entity.setOldGradeDiamond(logList.get(1).getGradeDiamond());
                entity.setOldGradeStar(logList.get(1).getGradeStar());
            }
        }
        return list;
    }

    public List<SecurityRiskLogEntity> selectLogList(Map<String, Object> map) {
        return riskMapper.selectLogList(map);
    }

    @Transactional
    public void insertList() {
        List<String> list = riskMapper.selectdwmcList();
        for (String item : list) {
            SecurityRiskEntity entity = new SecurityRiskEntity();
            entity.setDwmc(item);
            entity.setGradeDiamond(0);
            entity.setGradeStar(0);
            entity.setHangtag(0);
            entity.setCreateBy("admin");
            riskMapper.insert(entity);
        }
    }

    @Transactional
    public int update(SecurityRiskLogEntity logEntity) {
        riskMapper.insertLog(logEntity);
        SecurityRiskEntity entity = new SecurityRiskEntity();
        entity.setId(logEntity.getRiskId());
        entity.setEvaluate(logEntity.getEvaluate());
        entity.setUpdateBy(logEntity.getCreateBy());
        if (logEntity.getType() == 1) {
            entity.setGradeDiamond(logEntity.getGradeDiamond());
            entity.setGradeStar(logEntity.getGradeStar());
        } else {
            entity.setHangtag(logEntity.getHangtag());
        }
        return riskMapper.update(entity);
    }

}
